/*
 * ReplaceDialog.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Felix(felix@ulise.cs.pub.ro)
 * 
 * Obs: N/A
 */

package ide;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.metal.*;


@SuppressWarnings("serial")
public class ReplaceDialog extends JDialog {
	public JTextField replaceField = new JTextField();
	public JTextField findField = new JTextField();
	public VeriSimDocumentFrame doc;
	public int fromWhereToFind = 0;
	public String textToFind = new String("");
	public String textToReplace = new String("");
	public int primaData = 1;

	public JDialog thisDialog;


	public ReplaceDialog(JFrame f, VeriSimDocumentFrame doc) {
		super(f, "Replace", true);

		this.doc = doc;

		JPanel inputPanel= new JPanel();
		inputPanel.setLayout( new GridLayout(2,2) );

		inputPanel.add(new Label("Find what:"));
		inputPanel.add(findField);
		inputPanel.add(new Label("Replace with:"));
		inputPanel.add(replaceField);

		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout ( new FlowLayout(FlowLayout.CENTER) );


		JButton replace = new JButton("Replace");
		JButton replaceAll = new JButton("Replace All");
		JButton findNext = new JButton("Find Next");
		JButton cancel = new JButton("Cancel");
		inputPanel.add(findNext);
		inputPanel.add(replace);
		inputPanel.add(replaceAll);
		inputPanel.add(cancel);

		thisDialog = this;

		replace.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				replace();
				if(!find(findField.getText())) {
					JOptionPane.showMessageDialog(thisDialog, "No more matches for specified text.");
					textToFind = "";
					fromWhereToFind = 0;
				}
			}});

		replaceAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				while(find(findField.getText())) replace();
			}});

		findNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!find(findField.getText())) {
					JOptionPane.showMessageDialog(thisDialog, "No more matches for specified text.");
					textToFind = "";
					fromWhereToFind = 0;

				}
			}});

		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				textToFind = "";
				fromWhereToFind = 0;
			}});



		getRootPane().setDefaultButton(findNext);
		getContentPane().setLayout(new BorderLayout());


		getContentPane().add(inputPanel, "Center");
		getContentPane().add(buttonPanel, "South");

		pack();
		centerDialog();
	}

	protected boolean find(String textToFind) {
		if(!textToFind.equals(findField.getText().trim())){
			fromWhereToFind = 0;
			textToFind = findField.getText().trim();

		}
		if(findField.getText() == ""){
			JOptionPane.showMessageDialog(thisDialog, "No text to find specified.");
			fromWhereToFind = 0;
		}


		int n = doc.editor.getText().indexOf(textToFind,fromWhereToFind);
		if (n == -1) return false;
		fromWhereToFind = n + textToFind.length();;
		doc.editor.select(n,n + textToFind.length()); 
		return true;

	}

	protected  void replace(){
		doc.editor.replaceSelection(replaceField.getText());
	}


	protected void centerDialog() {
		Dimension screenSize = this.getToolkit().getScreenSize();
		Dimension size = this.getSize();
		screenSize.height = screenSize.height/2;
		screenSize.width = screenSize.width/2;
		size.height = size.height/2;
		size.width = size.width/2;
		int y = screenSize.height - size.height;
		int x = screenSize.width - size.width;
		this.setLocation(x,y);
	}
}
